#include "led_control.h"
#include "settings.h"
#include "mcp_server.h"
#include <esp_log.h>
#include <driver/gpio.h>

#define TAG "LedControl"

LedControl::LedControl(gpio_num_t gpio_num) 
    : gpio_num_(gpio_num), is_on_(false), brightness_(100) {
    
    // 配置GPIO为输出模式
    gpio_config_t io_conf = {};
    io_conf.pin_bit_mask = (1ULL << gpio_num);
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    io_conf.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&io_conf);
    
    // 初始状态：关闭
    gpio_set_level(gpio_num, 0);
    
    // 从设置中读取亮度
    Settings settings("led");
    brightness_ = settings.GetInt("brightness", 100);  // 默认100%亮度
    is_on_ = settings.GetBool("state", false);         // 默认关闭状态
    
    // 应用初始状态
    if (is_on_) {
        gpio_set_level(gpio_num, 1);
    }

    ESP_LOGI(TAG, "LED control initialized on GPIO %d. State: %s, Brightness: %d%%", 
             gpio_num, is_on_ ? "ON" : "OFF", brightness_);

    auto& mcp_server = McpServer::GetInstance();
    
    // 获取GPIO号
    mcp_server.AddTool("self.led.get_gpio",
        "Get the GPIO number used by LED",
        PropertyList(), [this](const PropertyList&) -> ReturnValue {
            return static_cast<int>(gpio_num_);
        });
    
    // 获取当前状态
    mcp_server.AddTool("self.led.get_state",
        "Get the current state of the LED (on/off)",
        PropertyList(), [this](const PropertyList&) -> ReturnValue {
            return is_on_ ? "on" : "off";
        });
    
    // 开灯
    mcp_server.AddTool("self.led.on",
        "Turn on the LED",
        PropertyList(), [this](const PropertyList&) -> ReturnValue {
            TurnOn();
            return true;
        });
    
    // 关灯
    mcp_server.AddTool("self.led.off",
        "Turn off the LED",
        PropertyList(), [this](const PropertyList&) -> ReturnValue {
            TurnOff();
            return true;
        });
    
    // 切换状态
    mcp_server.AddTool("self.led.toggle",
        "Toggle the LED state",
        PropertyList(), [this](const PropertyList&) -> ReturnValue {
            if (is_on_) {
                TurnOff();
            } else {
                TurnOn();
            }
            return true;
        });
    
    // 获取亮度
    mcp_server.AddTool("self.led.get_brightness",
        "Get the brightness of the LED (0-100)",
        PropertyList(), [this](const PropertyList&) -> ReturnValue {
            return brightness_;
        });
    
    // 设置亮度
    mcp_server.AddTool("self.led.set_brightness",
        "Set the brightness of the LED (0-100)",
        PropertyList({
            Property("percent", kPropertyTypeInteger, 0, 100)
        }), [this](const PropertyList& properties) -> ReturnValue {
            int percent = properties["percent"].value<int>();
            SetBrightness(percent);
            return true;
        });
}

LedControl::~LedControl() {
    // 关灯
    TurnOff();
}

void LedControl::TurnOn() {
    if (!is_on_) {
        gpio_set_level(gpio_num_, 1);
        is_on_ = true;
        ESP_LOGI(TAG, "LED turned ON on GPIO %d", gpio_num_);
        
        // 保存状态
        Settings settings("led", true);
        settings.SetBool("state", true);
    }
}

void LedControl::TurnOff() {
    if (is_on_) {
        gpio_set_level(gpio_num_, 0);
        is_on_ = false;
        ESP_LOGI(TAG, "LED turned OFF on GPIO %d", gpio_num_);
        
        // 保存状态
        Settings settings("led", true);
        settings.SetBool("state", false);
    }
}

void LedControl::SetBrightness(int percent) {
    if (percent < 0) percent = 0;
    if (percent > 100) percent = 100;
    
    brightness_ = percent;
    ESP_LOGI(TAG, "Set brightness to %d%% on GPIO %d", percent, gpio_num_);
    
    // 实际应用中，这里可以添加PWM调光代码
    // 当前实现中，亮度只影响状态记忆，不影响GPIO输出
    
    // 保存亮度设置
    Settings settings("led", true);
    settings.SetInt("brightness", brightness_);
}